﻿#include <cstring>
#include <iostream>
using namespace std;

#define for_m(a,b) for(int m=a;m<b;m++)
#define for_n(a,b) for(int n=a;n<b;n++)

int pos[4][8][2] = {
    {{0,1},{0,2},{0,3},{0,4},{0,-1},{0,-2},{0,-3},{0,-4}},
    {{1,0},{2,0},{3,0},{4,0},{-1,0},{-2,0},{-3,0},{-4,0}},
    {{1,1},{2,2},{3,3},{4,4},{-1,-1},{-2,-2},{-3,-3},{-4,-4}},
    {{1,-1},{2,-2},{3,-3},{4,-4},{-1,1},{-2,2},{-3,3},{-4,4}}
};

char map[19][19];
int a, b, c;

void play();
void change(int, int);
void show();
void init();
bool check_w(int, int);
bool check_b(int, int);
bool check_3(int, int);

string choose;
bool finish = false;
int main()
{
    init();
    while (!finish)
    {
        play();
    }
}

void play() {
    cout << "请输入落子位置及黑白区分（如0 2 2或1 3 3，0为黑，1为白）" << endl;
    cin >> c >> a >> b;
    if (c != 1 && c != 0)
        cout << "重输";
    else if (a > 19 || a < 0 || b>19 || b < 0)
    {
        cout << "超出棋盘范围，请重试" << endl;
    }
    else {
        change(a, b);
        if (c == 0)
        {
            if (check_b(a, b))
                cout << "黑方胜！" << endl;
            show();
        }
        else
        {
            if (check_w(a, b))
                cout << "白方赢！" << endl;
            show();
        }
    }
}


void change(int a, int b) {
    if (map[a][b] == '-') {
        if (c == 0)
        {
            map[a][b] = '0';
        }
        else
        {
            map[a][b] = '$';
        }
    }
    else {
        cout << "已有棋子，请重下" << endl;
    }
}

void init() {
    cout << "棋盘大小为10*10" << endl;
    for (int i = 0; i < 19; i++)
    {
        for (int m = 0; m < 19; m++)
        {
            map[i][m] = '-';
            cout << map[i][m] << " ";
        }
        cout << endl;
    }
}

void show() {
    for (int i = 0; i < 19; i++)
    {
        for (int m = 0; m < 19; m++)
        {
            cout << map[i][m] << " ";
        }
        cout << endl;
    }
}

bool check_b(int x, int y) {
    for_m(0, 4) {
        int count = 0;
        for_n(0, 4) {
            int mapx = x + pos[m][n][0];
            int mapy = y + pos[m][n][1];
            if (mapx >= 0 && mapx < 19 && mapy >= 0 && mapy < 19 && map[mapx][mapy] == '0')
            {
                count++;
            }
            else
                break;
        }

        for_n(4, 8) {
            int mapx = x + pos[m][n][0];
            int mapy = y + pos[m][n][1];
            if (mapx >= 0 && mapx < 19 && mapy >= 0 && mapy < 19 && map[mapx][mapy] == '0')
            {
                count++;
            }
            else
                break;
        }
        if (count >= 4)
            return 1;
    }
    return 0;
}

bool check_w(int x, int y) {
    for_m(0, 4) {
        int count = 0;
        for_n(0, 4) {
            int mapx = x + pos[m][n][0];
            int mapy = y + pos[m][n][1];
            if (mapx >= 0 && mapx < 19 && mapy >= 0 && mapy < 19 && map[mapx][mapy] == '$')
            {
                count++;
            }
            else
                break;
        }

        for_n(4, 8) {
            int mapx = x + pos[m][n][0];
            int mapy = y + pos[m][n][1];
            if (mapx >= 0 && mapx < 19 && mapy >= 0 && mapy < 19 && map[mapx][mapy] == '$')
            {
                count++;
            }
            else
                break;
        }
        if (count >= 4)
            return 1;
    }
    return 0;
}

bool check_3(int x, int y) {
    return 0;
}